<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/30
 * Time: 10:50
 */

namespace app\common\model;



use app\index\validate\Register;
use think\Loader;
use think\Model;

/**
 * Class Users
 * @package app\common\model
 * 前台用户模型
 */
class Users extends Model
{
    //连接数据库
    protected $table= 'shop_user';
    protected $pk= 'uid';
    //注册
    public function setPasswordAttr($data)
    {
        return MD5($data);
    }
    public function register($data){
// 调用Member验证器类进行数据验证
        $vailDate =Loader::validate('Register');
        if(!$vailDate->check($data)){
            return['valid'=>0,'msg'=>$vailDate->getError()];
        }else{
            $this->allowField(true)->save($data);
            return ['valid'=>1,'msg'=>'注册成功'];
        }
    }

    //登录
    public function login($data){
        $vailDate =Loader::validate('login');
        if(!$vailDate->check($data)){
            return['valid'=>0,'msg'=>$vailDate->getError()];
        }
        //登录判断
        $userInfo=$this->where('uname',$data['uname'])->where('password',md5($data['password']))->find();
        if(!$userInfo){
            return['valid'=>0,'msg'=>'用户名或密码错误'];
        }
        //3.登录成功
        session('index.username',$userInfo['uname']);
        session('index.uid',$userInfo['uid']);
        return ['valid'=>1,'msg'=>'登录成功'];
    }
}